package com.peaker.advertisingpro.utils

import android.annotation.SuppressLint
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/4/15.
 *  代码人生
 */
object DateUtils {

    @Throws(ParseException::class)
    fun transferFormat(inTime: String?): String? {
        val s1 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val s2 = SimpleDateFormat("HH:mm")
        var tempDate: Date? = null
        var outTime: String? = null
        tempDate = s1.parse(inTime)
        outTime = s2.format(tempDate)
        return outTime
    }

    /**
     * 转换为毫秒
     */
    fun transferMs(inTime: String?): Long {

        val s1 = SimpleDateFormat("yyyy-MM-dd HH:mm")
        var tempDate: Date? = null
        tempDate = s1.parse(getCurrentDate() + " " + inTime)
        return tempDate.time
    }

    fun getCurrentTime(format: String = "yyyy-MM-dd HH:mm"): String {
//        val s1 = SimpleDateFormat("yyyy-MM-dd HH:mm")
        val s1 = SimpleDateFormat(format)
        var format = s1.format(Date())
        return format
    }

    fun getCurrentDate(format: String = "yyyy-MM-dd"): String {
//        val s1 = SimpleDateFormat("yyyy-MM-dd HH:mm")
        val s1 = SimpleDateFormat(format)
        var format = s1.format(Date())
        return format
    }


    /**
     *
     * @param nowTime   当前时间
     * @param startTime    开始时间
     * @param endTime   结束时间
     * @return
     * @author sunran   判断当前时间在时间区间内
     */
    fun isEffectiveDate(nowTime: Date, startTime: Date, endTime: Date): Boolean {
//        nowTime.time.
        if (startTime.time == nowTime.time || nowTime.time == endTime.time) {
            return true
        }
        val date = Calendar.getInstance()
        date.time = nowTime
        val begin = Calendar.getInstance()
        begin.time = startTime
        val end = Calendar.getInstance()
        end.time = endTime
        return date.after(begin) && date.before(end)
    }

    /**.
     * 将字符串转换为date
     * @return Date
     */
    @SuppressLint("SimpleDateFormat")
    fun getDateByStr(str: String): Date {
        var format = "yyyy-MM-dd"
        val s1 = SimpleDateFormat(format)
        return s1.parse(str)
    }

    fun getDateByStr2(str: String): Date {
        var format = "yyyy-MM-dd HH:mm:ss"
        val s1 = SimpleDateFormat(format)
        return s1.parse(str)
    }

    /**
     * 将时分 装换为 date
     * @param hm String
     */
    fun getDateByHm(hm: String): Date {
        val s3 = SimpleDateFormat("yyyy-MM-dd")
        val format = s3.format(Date())
        val s1 = SimpleDateFormat("yyyy-MM-dd HH:mm")
        return s1.parse("$format $hm")
    }

}